package org.jix.framework.common.util;

import cn.hutool.core.collection.Partition;

import java.util.regex.Pattern;

/**
 * @author stark
 * @date 2025/6/21 12:05
 * @description 参数校验
 **/
public final class ParamUtils {

    private ParamUtils() {

    }

    //=======================校验昵称==========================//
    //自定义昵称长度范围
    private static final int NICK_NAME_MIN_LENGTH = 1;
    private static final int NICK_NAME_MAX_LENGTH = 20;

    //定义特殊字符的正则表达式
    private static final String NICK_NAME_REGEX = "[!@#$%^&*(),.?\":{}|<>]";

    /**
     * 昵称校验
     * @param nickName
     * @return
     */
    public static boolean checkNickName( String nickName) {
        //检查长度
        if (nickName.length() < NICK_NAME_MIN_LENGTH || nickName.length() > NICK_NAME_MAX_LENGTH) {
            return false;
        }

        //检查是否有特殊字符
        Pattern pattern = Pattern.compile(NICK_NAME_REGEX);
        return !pattern.matcher(nickName).find();
    }

    //=======================校验小黄书号==========================//
    //定义ID长度范围
    private static final int ID_MIN_LENGTH = 5;
    private static final int ID_MAX_LENGTH = 15;

    //定义正则表达式
    private static final String ID_REGEX = "^[a-zA-Z0-9_]+$";

    /**
     * 小黄书ID校验
     * @param gbookId
     * @return
     */
    public static boolean checkGbookId( String gbookId) {
        //检查长度
        if (gbookId.length() < ID_MIN_LENGTH || gbookId.length() > ID_MAX_LENGTH) {
            return false;
        }
        //检查格式
        Pattern pattern = Pattern.compile(ID_REGEX);
        return pattern.matcher(gbookId).matches();
    }

    /**
     * 字符串长度校验
     * @param str
     * @param length
     * @return
     */
    public static boolean checkLength(String str, int length) {
        // 检查长度
        if (str.isEmpty() || str.length() > length) {
            return false;
        }
        return true;
    }
}
